/*
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-04-01 09:30:53
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2024-12-19 09:45:04
 * @FilePath: \Backstage\WebsiteUI\src\api\favors.js
 */
import request from '@/utils/request';

// 获取人情列表
export function getFavorsList(data) {
  return request({
    url: '/favors/list',
    method: 'get',
    params: data
  });
}

// 修改人情回礼
export function editFavors(data) {
  return request({
    url: '/favors/edit',
    method: 'post',
    data
  });
}

// 修改人情回礼
export function addFavors(data) {
  return request({
    url: '/favors/add',
    method: 'post',
    data
  });
}

// 修改人情回礼
export function delFavors(id) {
  return request({
    url: `/favors/del?id=${id}`,
    method: 'delete'
  });
}

// 导出人情回礼
export function exportFavors(data) {
  return request({
    url: '/favors/export',
    method: 'get',
    params: data,
    responseType: 'blob'
  });
}

// 下载模板
export function downloadTemplate() {
  return request({
    url: '/favors/template',
    method: 'get',
    responseType: 'blob'
  });
}

// 导入人情回礼
export function importFavors({ file, role_key, role_str, name = '' }) {
  let formData = new FormData();
  let suffix = file.name && file.name.split('.')[1];
  let defaultName = file.name && file.name.split('.')[0];
  let fileName = name ? name.replace(/[\u4e00-\u9fa5]/g, '') + '.' + suffix : defaultName.replace(/[\u4e00-\u9fa5]/g, '') + '.' + suffix;
  formData.append('file', file, fileName);
  formData.append('role_key', role_key);
  formData.append('role_str', role_str);
  return request({
    url: '/favors/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}
